package com.java.advance.order.server.service.impl;

import com.java.advance.order.server.dao.MsgMapper;
import com.java.advance.order.server.dao.OrderMapper;
import com.java.advance.order.server.domain.pojo.Msg;
import com.java.advance.order.server.domain.pojo.Order;
import com.java.advance.order.server.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by yangminghua on 2019/4/12.
 */
@Slf4j
@Service("OrderService")
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MsgMapper msgMapper;

    @Override
    @Transactional
    public void addOrder(Order orderInfo) {
        //写订单表
        orderMapper.insert(orderInfo);
        //写订单详情表
        //TODO...
        try {
            sendMsg(orderInfo);
        } catch (Exception e) {
            log.error("发送短信异常");
        }
    }

    @Override
    @Transactional
    public void sendMsg(Order orderInfo) {
        log.info("发送短信");
        //拼装短信内容
        Msg msg = new Msg("尊敬的" + orderInfo.getUserId() + "用户，您的订单" + orderInfo.getOrderId() + "请尽快付款哦～", 1, "wait_send", new Date());
        msgMapper.insert(msg);
        //TODO 调用短信网关
    }

    @Override
    public void init() {
        orderMapper.deleteAll();
        msgMapper.deleteAll();
    }
}
